shared.service('AuthorityService', function ($q, $http, URLService, $cookies) {
    this.setRole = function (role) {
        var roleJson = JSON.stringify(role);
        $cookies.put('role', roleJson, {path: '/'});
    };

    this.getRole = function () {
        var role = $cookies.get('role');
        return role ? JSON.parse(role) : null;
    };

    this.removeRole = function () {
        $cookies.remove('role', {path: '/'});
    };

    this.check = function () {
        if (!this.isLogin()) {
            window.location = SYSCONF.index_url;
        }
    };

    this.quit = function () {
        var _this = this;
        var deferred = $q.defer();
        var url = URLService.getURL('logout');
        $http.post(url).success(function (res) {
            if (res.success) {
                _this.removeRole();
                deferred.resolve();
            } else {
                deferred.reject();
            }
        });
        return deferred.promise;
    };

    this.isLogin = function () {
        return !!this.getRole();
    };
});